package grid;


public abstract class AbstractGrid {
	public int nX;
	public int nY;
	public int nZ;
	public int nYZ;
	public int n;
public float x1;
public float y1;
public float z1;
public float dim;

public void update(){
	this.nYZ=nY*nZ;
	this.n=nX*nY*nZ;
}
/**
 * returns the index from position x,y,z
 * @param x
 * @param y
 * @param z
 * @return
 */
public int getIndex(int x,int y,int z){
	return x*nYZ+y*nZ+z;
}
/**
 * @param index
 * @return
 */
public int getX(int index){
	return index/nYZ;
}
/**
 * @param index
 * @return
 */
public int getY(int index){
	return (index/nZ)%nY;
}
/**
 * @param index
 * @return
 */
public int getZ(int index){
	return index%nZ;
}

/**
 * @param x
 * @return
 */
public float getXFloat(int x){
	return x1+x*dim;
}
/**
 * @param y
 * @return
 */
public float getYFloat(int y){
	return y1+y*dim;
}
/**
 * @param z
 * @return
 */
public float getZFloat(int z){
	return z1+z*dim;
}
public int getXInt(float x){
	return (int)((x-x1)/dim);
}
/**
 * @param y
 * @return
 */
public  int getYInt(float y){
	return (int)((y-y1)/dim);
}
/**
 * @param x
 * @return
 */
/**
 * @param z
 * @return
 */
public int getZInt(float z){
	return (int)((z-z1)/dim);
}

public float x1(){
	return x1;
}
public float y1(){
	return y1;
}
public float z1(){
	return z1;
}
public float x2(){
	return x1+nX*dim;
}
public float y2(){
	return y1+nY*dim;
}
public float z2(){
	return z1+nZ*dim;
}
public float cX(){
	return (x1()+x2())*0.5f;
}
public float cY(){
	return  (y1()+y2())*0.5f;
}
public float cZ(){
	return  (z1()+z2())*0.5f;
}
public static float map(float v, float s1, float s2, float t1, float t2){
	if (s1==s2)return t1;
	return (t2-t1)*(v-s1)/(s2-s1)+t1;
}
}
